/**
 * 社区动态js
 */
aMailServices.factory('trends', function ($http) {
     var trends = function () {
    	 this.banners = [];
    	 this.top_news = [];
    	 this.top_acticle = [];
    	 this.baseUrl = ip+"/";
    	 this.base = ip;
    	 this.adUrl = ip+"/"+"data/afficheimg/";
         this.busy = false;
         this.wxid = 0;
     };
     trends.prototype.initTrends = function(){
    	 if (this.busy){
        	 hideSpin();
        	 return;
         }
         this.busy = true;
         var url = cl.getTrendsUrl+"&callback=JSON_CALLBACK" ;
         $http.jsonp(url).success(function (res) {
				if(res.code=="CL02023"){//获取成功
					hideSpin();
					this.top_news = res.result.top_news;
					this.banners = res.result.ad;
					this.top_acticle = res.result.top_acticle;
					this.wxid = res.result.wxid;
				}
				setTimeout("initBox()",1000);
				this.busy = false;
         }.bind(this));
     };
     trends.prototype.collectNews = function(news_id,index){
    	 if (this.busy){
        	 return;
         }
    	 if(!this.wxid>0)
         {
    		 showAlert("登录后方可收藏");
    		 return;
         }
    	 this.busy = true;
         var url = cl.collectNewUrl+"&wxid="+this.wxid+"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
         $http.jsonp(url).success(function (res) {
				if(res.code=="CL02026"){//获取成功
					if(res.result.codeMsg=="cancel"){
						showAlert("取消收藏");
						this.top_news[index].is_collect = " ";
					}else{
						showAlert("已加入收藏");
						this.top_news[index].is_collect = "collected";
					}
				}else{
					showAlert("收藏失败，请登录后再操作");
				}
				this.busy = false;
         }.bind(this));
     };
     return trends;
 });

//动态controller
function TrendsController($scope, $routeParams,$rootScope,$location,trends) {
	showSpin();
	rightMenu();
	$scope.trends = new trends();
	$scope.trends.initTrends();
}


aMailServices.factory('collect', function ($http,$rootScope) {
    var collect = function () {
		this.my_collect = [];
		this.baseUrl = ip+"/";
		this.adUrl = ip+"/"+"data/afficheimg/";
		this.busy = false;
		this.wxid = 0;
    };
    collect.prototype.initCollect = function(){
	   	 if (this.busy){
	       	 hideSpin();
	       	 return;
	        }
	    this.busy = true;
	    var pro = this;
   	 var url = cl.getMyCollectUrl+"&callback=JSON_CALLBACK" ;
        $http.jsonp(url).success(function (res) {
				if(res.code=="CL02024"){//获取成功
					this.my_collect = res.result.res;
					 if(this.my_collect.length==0){
			      		   $rootScope.changeNull();
			      	  }else{
			      		  $rootScope.hideNull();
			      	  }
					this.wxid = res.result.wxid;
				}else{
					showAlert("请登录后再次进入");
					$rootScope.changeNull();
//					$rootScope.go('back', 'fade');
				}
				hideSpin();
				this.busy = false;
        }.bind(this));
    };
    collect.prototype.collectNews = function(news_id,index){
   	 if (this.busy){
       	 return;
        }
   	 if(!this.wxid>0)
        {
   		 showAlert("登录后方可操作");
   		 return;
        }
   	 this.busy = true;
        var url = cl.collectNewUrl+"&wxid="+this.wxid+"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
        $http.jsonp(url).success(function (res) {
				if(res.code=="CL02026"){//获取成功
					if(res.result.codeMsg=="cancel"){
						showAlert("取消收藏");
						this.my_collect[index].is_collect = "重新关注";
					}else{
						showAlert("已加入收藏");
						this.my_collect[index].is_collect = "移除关注";
					}
				}else{
					showAlert("收藏失败，请登录后再操作");
				}
				this.busy = false;
        }.bind(this));
    };
    return collect;
});

//我的收藏controller
function CollectController($scope, $routeParams,$rootScope,$location,collect) {
	showSpin();
	rightMenu();
	$scope.collect = new collect();
	$scope.collect.initCollect();
}

aMailServices.factory('picture', function ($http,$rootScope) {
    var picture = function (newsId) {
		this.news_info={};
		this.imgs = [];
		this.likes = [];
		this.comments = [];
		this.baseUrl = ip+"/";
		this.adUrl = ip+"/"+"data/afficheimg/";
		this.busy = false;
		this.wxid = 0;
		this.news_show = false;
		this.news_id = newsId;
    };
    picture.prototype.getNewsInfo = function(){
	   	 if (this.busy){
	       	 hideSpin();
	       	 return;
	        }
	    this.busy = true;
	    var pro = this;
   	 var url = cl.getNewsInfoUrl+"&news_id="+pro.news_id+"&callback=JSON_CALLBACK" ;
        $http.jsonp(url).success(function (res) {
				if(res.code=="CL02031"){//获取成功
					pro.news_info = res.result.news;
					pro.imgs = res.result.img;
					pro.likes = res.result.likes;
					pro.comments = res.result.comment;
					pro.wxid = res.result.wxid;
					pro.news_show = true;
				}else{
					showAlert("获取不到该帖信息，可能已被删除");
					$rootScope.changeNull();
				}
				hideSpin();
				this.busy = false;
        }.bind(this));
    };
    return picture;
});
//随手拍详情controller
function PictureInfoController($scope, $routeParams,$rootScope,$location,picture) {
	showSpin();
	rightMenu();
	$scope.picture =new picture($routeParams.newsId);
	$scope.picture.getNewsInfo();
}